﻿define(["jquery", "jqueryValid", "jqueryValidMethods", "common", "capsLockTip"], function ($, jqV, jqM, common) {
    //刷新验证码
    var reV = function (obj) {
        obj.attr("src", "/User/CreateCode?id=" + Math.random());
    }

    $(".login_password").capsLockTip();

    //回车事件
    var enterClick = function () {
        $(document).keydown(function (event) {
            if (event.keyCode == 13) {
                $("#login").click();
            }
        });
    }
    //登陆
    var login = function (configErrorCount) {
        if ($("#loginForm").valid()) {
            if (!common.checkSqlInject())
                return;

            var userName = $("#UserName").val();
            var password = $("#Password").val();
            var validationCode = $("#ValidationCode").val();
            $.ajax({
                url: "/User/Login",
                type: "POST",
                data: { UserName: userName, Password: password, ValidationCode: validationCode },
                success: function (data) {
                    var isValid = data.isValid;
                    var type = data.type;
                    var msg;
                    if (isValid) {
                        if (type != null && type != "" && type != "0") {
                            if (type == "Successful") {
                                window.location.href = "/Home/Index";
                                return;
                            }
                            switch (type) {
                                case "OANetError":
                                    msg = "OA登录服务调用失败，请联系管理员！";
                                    break;
                                case "ErrorValidCode":
                                    msg = "您输入的验证码错误，请确认后重新输入！";
                                    break;
                                case "SystemError":
                                    msg = "系统异常！";
                                    break;
                                case "WrongPassword":
                                    msg = "账号或密码错误，请确认后重新输入！";
                                    break;
                                case "WrongNameOrPassword":
                                    msg = "账号或密码错误，请确认后重新输入！";
                                    break;
                                case "IncompleteData":
                                    msg = "资料不完整！";
                                    break;
                                case "UnauthorizedAuthority":
                                    msg = "您输入的账号暂未开通权限！";
                                    break;
                                case "UnauthorizedArea":
                                    msg = "未授权区域！";
                                    break;
                            }
                        }
                    } else {
                        msg = data.Msg;
                    }
                    reV($("#captcha"));
                    alert(msg);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    if (XMLHttpRequest.readyState == 0) {
                        alert("您的网络不给力，请检查网络后重新尝试登录!");
                    }
                }
            });
        }
    }
    //计算
    var reset = function () {
        var w = document.documentElement.clientHeight;
        var w_top = (w - 520) / 2;
        var loginPage = document.getElementById('loginPage');
        loginPage.setAttribute('style', 'margin-top:' + w_top + 'px');
    }
    return {
        errorCount: 0,
        init: function () {
            var errorCount = this.errorCount;
            reset();
            $(window).resize(function () {
                reset();
            });
            reV($("#captcha"));
            //初始化刷新验证码
            $("#captcha").click(function () {
                reV($("#captcha"));
            });
            //登陆绑定
            $("#login").click(function () {
                login(errorCount);
            });
            //绑定回车
            enterClick();

            $.validator.setDefaults({
                //光标移出时
                onfocusout: function (element) {
                    this.element(element);
                },

                //光标移入时
                onfocusin: function (element, event) {
                    //仅针对jquery.validate
                    var errorElement = this.errorsFor(element);
                    if (errorElement) {
                        errorElement.remove();
                    }
                },
                onkeyup: function (element, event) {
                }
            });

            //form验证
            $("#loginForm").validate({
                rules: {
                    UserName: {
                        required: true,
                        isDefault: "账号不能为空"
                    },
                    Password: {
                        required: true
                    },
                    ValidationCode: {
                        required: true,
                        minlength: 4,
                        maxlength: 4
                    }
                },
                messages: {
                    UserName: {
                        required: "账号不能为空",
                        isDefault: "账号不能为空"
                    },
                    Password: {
                        required: "密码不能为空"
                    },
                    ValidationCode: {
                        required: "验证码不能为空",
                        minlength: "验证码长度必须为4位",
                        maxlength: "验证码长度必须为4位"
                    }
                }
            });
        }
    };
});